/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.freedom.railhunter.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.freedom.railhunter.util.Des;
import org.freedom.railhunter.util.StationManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Alan
 */
public class OrderConfiguration {
    
    private static final Logger logger = LoggerFactory.getLogger(OrderConfiguration.class);
    
    public static final String ORDER_CONFIG_FILE_NAME = "order_ticket_config.properties";
    
    public static final String _12306_USERNAME_KEY = "_12306_username";
    public static final String _12306_PASSWORD_KEY = "_12306_password";
    public static final String UUWISE_USERNAME_KEY = "uuwise_username";
    public static final String UUWISE_PASSWORD_KEY = "uuwise_password";
    public static final String FROM_STATION_NAME_KEY = "from_station_name";
    public static final String TO_STATION_NAME_KEY = "to_station_name";
    public static final String TRAIN_DATE_KEY = "train_date";
    public static final String PASSENGERS_KEY = "passengers";
    public static final String TRAIN_CODES_KEY = "train_codes";
    public static final String SEAT_TYPES_KEY = "seat_types";
    public static final String AUTO_FILL_BAND_CODE_KEY = "auto_fill_band_code";
    
    public static final String MAIL_SENDER_USERNAME = "mail_sender_username";
    public static final String MAIL_SENDER_PASSWORD = "mail_sender_password";
    public static final String MAIL_SENDER_SERVER_HOST = "mail_sender_server_host";
    public static final String MAIL_SENDER_SERVER_PORT = "mail_sender_server_port";
    public static final String MAIL_RECIPIENT_USERNAME = "mail_recipient_username";
    
    
    private String _12306UserName = "";
    
    private String _12306Password = "";
    
    private String uuwiseUsername = "";
    
    private String uuwisePassword = "";
    
    private Station fromStation;
    
    private Station toStation;
    
    private Date trainDate = new Date();
    
    private List<String> passengers = new ArrayList<>();
    
    private List<String> trainCodeList = new ArrayList<>();
    
    private List<SeatType> seatTypeList = new ArrayList<>();
    
    private Properties orderConfigProps = new Properties();
    
    private boolean autoFillBandCode;
    
    private String mailSenderUsername;
    private String mailSenderPassword;
    private String mailSenderServerHost;
    private String mailSenderServerPort;
    private String mailRecipientUsername;
    
    private static OrderConfiguration orderConfiguration = new OrderConfiguration();
    
    private OrderConfiguration() {
    }
    
    public static OrderConfiguration getInstance() {
        if (orderConfiguration == null) {
            orderConfiguration = new OrderConfiguration();
        }
        return orderConfiguration;
    }
    
    public void storeOrderConfig() {
        storeOrderConfig(new File(System.getProperty("user.home") + File.separator + ORDER_CONFIG_FILE_NAME));
    }
    
    public void storeOrderConfig(String filePath) {
        storeOrderConfig(new File(filePath));
    }
    
    public void storeOrderConfig(File file) {
        try {
            orderConfigProps.store(new FileOutputStream(file), "");
        } catch (IOException ex) {
            logger.error(null, ex);
        }
    }
    
    public void loadOrderConfig() {
        loadOrderConfig(System.getProperty("user.home") + File.separator + ORDER_CONFIG_FILE_NAME);
    }
    
    public void loadOrderConfig(String filePath) {
        loadOrderConfig(new File(filePath));
    }
    
    public void loadOrderConfig(File file) {
        try {
            if (!file.exists()) {
                return;
            }
            orderConfigProps.load(new FileInputStream(file));
            this.set12306UserName(orderConfigProps.getProperty(_12306_USERNAME_KEY));
            this.set12306Password(orderConfigProps.getProperty(_12306_PASSWORD_KEY));
            this.setUuwiseUsername(orderConfigProps.getProperty(UUWISE_USERNAME_KEY));
            this.setUuwisePassword(orderConfigProps.getProperty(UUWISE_PASSWORD_KEY));
            String fromStationValue = orderConfigProps.getProperty(FROM_STATION_NAME_KEY);
            if (fromStationValue != null) {
                this.setFromStation(StationManager.NAME_STATION_MAP.get(fromStationValue));
            }
            String toStationValue = orderConfigProps.getProperty(TO_STATION_NAME_KEY);
            if (toStationValue != null) {
                this.setToStation(StationManager.NAME_STATION_MAP.get(toStationValue));
            }
            String trainDateValue = orderConfigProps.getProperty(TRAIN_DATE_KEY);
            if (trainDateValue != null) {
                DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    this.setTrainDate(format.parse(trainDateValue));
                } catch (ParseException ex) {
                    logger.error("日期格式不正确，应为yyyy-MM-dd，例如：2014-01-01", ex);
                }
            }
            
            String passengersValue = orderConfigProps.getProperty(PASSENGERS_KEY);
            if (StringUtils.isNotEmpty(passengersValue)) {
                String[] passengerNames = passengersValue.split(",");
                for (String passengerName : passengerNames) {
                    this.getPassengers().add(passengerName);
                }
            }
            
            String trainCodesValue = orderConfigProps.getProperty(TRAIN_CODES_KEY);
            String pTrainStr = " ";
            if (StringUtils.isNotEmpty(trainCodesValue)) {
                String[] trainCodes = trainCodesValue.split(",");
                for (String trainCode : trainCodes) {
                    pTrainStr += trainCode + " ";
                    this.getTrainCodeList().add(trainCode);
                }
            }
            logger.info("优先车次:" + pTrainStr);
            
            String seatTypesValue = orderConfigProps.getProperty(SEAT_TYPES_KEY);
            String pSeatStr = " ";
            if (StringUtils.isNotEmpty(seatTypesValue)) {
                String[] seatTypeNames = seatTypesValue.split(",");
                for (String seatTypeName : seatTypeNames) {
                    for (SeatType seatType : SeatType.values()) {
                        if (seatTypeName.equals(seatType.getName())) {
                            pSeatStr += seatType.getName() + " ";
                            this.getSeatTypeList().add(seatType);
                            break;
                        }
                    }
                }
            }
            logger.info("优先席别:" + pSeatStr);
            
            String autoFileBandCodeValue = orderConfigProps.getProperty(AUTO_FILL_BAND_CODE_KEY);
            autoFillBandCode = Boolean.valueOf(autoFileBandCodeValue);
            
            mailSenderUsername = orderConfigProps.getProperty(MAIL_SENDER_USERNAME);
            mailSenderPassword = orderConfigProps.getProperty(MAIL_SENDER_PASSWORD);
            mailSenderServerHost = orderConfigProps.getProperty(MAIL_SENDER_SERVER_HOST);
            mailSenderServerPort = orderConfigProps.getProperty(MAIL_SENDER_SERVER_PORT);
            mailRecipientUsername = orderConfigProps.getProperty(MAIL_RECIPIENT_USERNAME);
        } catch (FileNotFoundException ex) {
            logger.error(null, ex);
        } catch (IOException ex) {
            logger.error(null, ex);
        }
    }

    public String get12306UserName() {
        return _12306UserName;
    }

    public void set12306UserName(String _12306UserName) {
        this._12306UserName = _12306UserName;
    }

    public String get12306Password() {
        return _12306Password;
    }

    public void set12306Password(String _12306Password) {
        this._12306Password = _12306Password;
    }
    
    public String getUuwiseUsername() {
        return uuwiseUsername;
    }

    public void setUuwiseUsername(String uuwiseUsername) {
        this.uuwiseUsername = uuwiseUsername;
    }

    public String getUuwisePassword() {
        return uuwisePassword;
    }

    public void setUuwisePassword(String uuwisePassword) {
        this.uuwisePassword = uuwisePassword;
    }

    public Station getFromStation() {
        return fromStation;
    }

    public void setFromStation(Station fromStation) {
        this.fromStation = fromStation;
    }

    public Station getToStation() {
        return toStation;
    }

    public void setToStation(Station toStation) {
        this.toStation = toStation;
    }

    public Date getTrainDate() {
        return trainDate;
    }

    public void setTrainDate(Date trainDate) {
        this.trainDate = trainDate;
    }

    public List<String> getPassengers() {
        return passengers;
    }

    public void setPassengers(List<String> passengers) {
        this.passengers = passengers;
    }

    public List<String> getTrainCodeList() {
        return trainCodeList;
    }

    public void setTrainCodeList(List<String> trainCodeList) {
        this.trainCodeList = trainCodeList;
    }

    public List<SeatType> getSeatTypeList() {
        return seatTypeList;
    }

    public void setSeatTypeList(List<SeatType> seatTypeList) {
        this.seatTypeList = seatTypeList;
    }

    public Properties getOrderConfigProps() {
        return orderConfigProps;
    }

    public void setOrderConfigProps(Properties orderConfigProps) {
        this.orderConfigProps = orderConfigProps;
    }

    public boolean isAutoFillBandCode() {
        return autoFillBandCode;
    }

    public void setAutoFillBandCode(boolean autoFillBandCode) {
        this.autoFillBandCode = autoFillBandCode;
    }

    public String getMailSenderUsername() {
        return mailSenderUsername;
    }

    public void setMailSenderUsername(String mailSenderUsername) {
        this.mailSenderUsername = mailSenderUsername;
    }

    public String getMailSenderPassword() {
        return mailSenderPassword;
    }

    public void setMailSenderPassword(String mailSenderPassword) {
        this.mailSenderPassword = mailSenderPassword;
    }

    public String getMailSenderServerHost() {
        return mailSenderServerHost;
    }

    public void setMailSenderServerHost(String mailSenderServerHost) {
        this.mailSenderServerHost = mailSenderServerHost;
    }

    public String getMailSenderServerPort() {
        return mailSenderServerPort;
    }

    public void setMailSenderServerPort(String mailSenderServerPort) {
        this.mailSenderServerPort = mailSenderServerPort;
    }

    public String getMailRecipientUsername() {
        return mailRecipientUsername;
    }

    public void setMailRecipientUsername(String mailRecipientUsername) {
        this.mailRecipientUsername = mailRecipientUsername;
    }
    
    public static OrderConfiguration getOrderConfiguration() {
        return orderConfiguration;
    }

    public static void setOrderConfiguration(OrderConfiguration orderConfiguration) {
        OrderConfiguration.orderConfiguration = orderConfiguration;
    }
}
